package org.eoti.elec.db;

import org.eoti.mimir.MimirDB;
import org.eoti.mimir.MimirEntry;
import org.eoti.mimir.MimirException;
import org.eoti.spec.mimirdb.v1.DBMapping;
import org.eoti.spec.raijin.v1.CircuitConfig;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * The MIT License
 * <p/>
 * Copyright (c) 2009 Malachi de AElfweald (malachid@gmail.com)
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
public class CircuitDB
extends MimirDB<CircuitConfig, String>
{
	private NameComparator nameComparator = new NameComparator();
	private CircuitNameComparator circuitNameComparator = new CircuitNameComparator();
	
	/**
	 * Create an instance of a database
	 *
	 * @param registry MimirRegistry responsible for maintaining the mappings
	 * @param dbMapping specification for this database
	 *
	 * @throws org.eoti.mimir.SchemeValidationException if the scheme is invalid
	 * @throws org.eoti.mimir.SchemeNotFoundException if the scheme is not found
	 */
	public CircuitDB(CircuitRegistry registry, DBMapping dbMapping)
			throws MimirException
	{
		super(registry, dbMapping);
	}

	protected void initializeDB()
			throws MimirException
	{
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public String createID( String id )
	{
		return id;
	}

	protected String getID( CircuitConfig circuitConfig )
	{
		return circuitConfig.getName();
	}

	protected String getNextID( String last ){
		throw new UnsupportedOperationException("getNextID not supported");
	}

	protected Comparator<String> getIDComparator(){
		return nameComparator;
	}

	public void display(String circuitName)
			throws MimirException
	{
		getEntry(circuitName).display();
	}

	public void display(String circuitName, PrintStream out)
			throws MimirException
	{
		getEntry(circuitName).display(out);
	}

	public CircuitConfig read(String circuitName)
			throws MimirException
	{
		return getEntry(circuitName).read();
	}

	public void write(CircuitConfig circuit)
			throws MimirException
	{
		try{
			getEntry(circuit.getName()).write(circuit);
		} catch(MimirException e){
			super.getScheme().createEntry(circuit.getName(), circuit);
		}
	}

	public List<CircuitConfig> getCircuits()
			throws MimirException
	{
		return getCircuits(circuitNameComparator);
	}

	public List<CircuitConfig> getCircuits( Comparator<CircuitConfig> comparator)
			throws MimirException
	{
		ArrayList<CircuitConfig> circuits = new ArrayList<CircuitConfig>();
		for( MimirEntry<CircuitConfig,String> entry : super.getScheme().getAllEntries())
			circuits.add(entry.read());

		Collections.sort(circuits, comparator);
		return circuits;
	}

	public CircuitConfig createNew(String circuitName)
	{
		CircuitConfig circuit = new CircuitConfig();
		circuit.setName(circuitName);
		return circuit;
	}

	class NameComparator
	implements Comparator<String>
	{
		/**
		 * Compares its two arguments for order.  Returns a negative integer, zero, or a positive integer as the first argument
		 * is less than, equal to, or greater than the second.<p>
		 * <p/>
		 * In the foregoing description, the notation <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
		 * <i>signum</i> function, which is defined to return one of <tt>-1</tt>, <tt>0</tt>, or <tt>1</tt> according to
		 * whether the value of <i>expression</i> is negative, zero or positive.<p>
		 * <p/>
		 * The implementor must ensure that <tt>sgn(compare(x, y)) == -sgn(compare(y, x))</tt> for all <tt>x</tt> and
		 * <tt>y</tt>.  (This implies that <tt>compare(x, y)</tt> must throw an exception if and only if <tt>compare(y, x)</tt>
		 * throws an exception.)<p>
		 * <p/>
		 * The implementor must also ensure that the relation is transitive: <tt>((compare(x, y)&gt;0) &amp;&amp; (compare(y,
		 * z)&gt;0))</tt> implies <tt>compare(x, z)&gt;0</tt>.<p>
		 * <p/>
		 * Finally, the implementor must ensure that <tt>compare(x, y)==0</tt> implies that <tt>sgn(compare(x,
		 * z))==sgn(compare(y, z))</tt> for all <tt>z</tt>.<p>
		 * <p/>
		 * It is generally the case, but <i>not</i> strictly required that <tt>(compare(x, y)==0) == (x.equals(y))</tt>.
		 * Generally speaking, any comparator that violates this condition should clearly indicate this fact.  The recommended
		 * language is "Note: this comparator imposes orderings that are inconsistent with equals."
		 *
		 * @param s1 the first object to be compared.
		 * @param s2 the second object to be compared.
		 *
		 * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater
		 *         than the second.
		 *
		 * @throws NullPointerException if an argument is null and this comparator does not permit null arguments
		 * @throws ClassCastException   if the arguments' types prevent them from being compared by this comparator.
		 */
		public int compare( String s1, String s2 ){
			return s1.compareTo(s2);
		}
	}

	class CircuitNameComparator
	implements Comparator<CircuitConfig>
	{

		/**
		 * Compares its two arguments for order.  Returns a negative integer, zero, or a positive integer as the first argument
		 * is less than, equal to, or greater than the second.<p>
		 * <p/>
		 * In the foregoing description, the notation <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
		 * <i>signum</i> function, which is defined to return one of <tt>-1</tt>, <tt>0</tt>, or <tt>1</tt> according to
		 * whether the value of <i>expression</i> is negative, zero or positive.<p>
		 * <p/>
		 * The implementor must ensure that <tt>sgn(compare(x, y)) == -sgn(compare(y, x))</tt> for all <tt>x</tt> and
		 * <tt>y</tt>.  (This implies that <tt>compare(x, y)</tt> must throw an exception if and only if <tt>compare(y, x)</tt>
		 * throws an exception.)<p>
		 * <p/>
		 * The implementor must also ensure that the relation is transitive: <tt>((compare(x, y)&gt;0) &amp;&amp; (compare(y,
		 * z)&gt;0))</tt> implies <tt>compare(x, z)&gt;0</tt>.<p>
		 * <p/>
		 * Finally, the implementor must ensure that <tt>compare(x, y)==0</tt> implies that <tt>sgn(compare(x,
		 * z))==sgn(compare(y, z))</tt> for all <tt>z</tt>.<p>
		 * <p/>
		 * It is generally the case, but <i>not</i> strictly required that <tt>(compare(x, y)==0) == (x.equals(y))</tt>.
		 * Generally speaking, any comparator that violates this condition should clearly indicate this fact.  The recommended
		 * language is "Note: this comparator imposes orderings that are inconsistent with equals."
		 *
		 * @param c1 the first object to be compared.
		 * @param c2 the second object to be compared.
		 *
		 * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater
		 *         than the second.
		 *
		 * @throws NullPointerException if an argument is null and this comparator does not permit null arguments
		 * @throws ClassCastException   if the arguments' types prevent them from being compared by this comparator.
		 */
		public int compare( CircuitConfig c1, CircuitConfig c2 ){
			return c1.getName().compareTo(c2.getName());
		}
	}
}
